Completed
Pull Request — develop (#209)
by Xaver
32s
created

node.js ➔ ... ➔ ips.forEach   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
c 0
b 0
f 0
nc 4
dl 0
loc 11
rs 9.4285
nop 2
1
define(['sorttable', 'snabbdom', 'd3-interpolate', 'moment', 'helper', 'utils/node'],
2
  function (SortTable, V, d3Interpolate, moment, helper, nodef) {
3
    'use strict';
4
    V = V.default;
5
6
    function showStatImg(o, d) {
7
      var subst = {};
8
      subst['{NODE_ID}'] = d.node_id;
9
      subst['{NODE_NAME}'] = d.hostname.replace(/[^a-z0-9\-]/ig, '_');
10
      subst['{TIME}'] = d.lastseen.format('DDMMYYYYHmmss');
11
      subst['{LOCALE}'] = _.locale();
12
      return helper.showStat(V, o, subst);
13
    }
14
15
    return function (el, d, linkScale, nodeDict) {
16
      function nodeLink(node) {
17
        return V.h('a', {
18
          props: {
19
            className: node.is_online ? 'online' : 'offline',
20
            href: router.generateLink({ node: node.node_id })
21
          }, on: {
22
            click: function (e) {
23
              router.fullUrl({ node: node.node_id }, e);
24
            }
25
          }
26
        }, node.hostname);
27
      }
28
29
      function nodeIdLink(nodeId) {
30
        if (nodeDict[nodeId]) {
31
          return nodeLink(nodeDict[nodeId]);
32
        }
33
        return nodeId;
34
      }
35
36
      function showGateway(node) {
37
        var gatewayCols = [
38
          V.h('span', [
39
            nodeIdLink(node.gateway_nexthop),
40
            V.h('br'),
41
            _.t('node.nexthop')
42
          ]),
43
          V.h('span', { props: { className: 'ion-arrow-right-c' } }),
44
          V.h('span', [
45
            nodeIdLink(node.gateway),
46
            V.h('br'),
47
            'IPv4'
48
          ]),
49
          V.h('span', [
50
            nodeIdLink(node.gateway6),
51
            V.h('br'),
52
            'IPv6'
53
          ])
54
        ];
55
56
        return V.h('td', { props: { className: 'gateway' } }, gatewayCols);
57
      }
58
59
      function renderNeighbourRow(n) {
60
        var icons = '';
61
        if (helper.hasLocation(n.node)) {
62
          icons = V.h('span', { props: { className: 'ion-location' } });
63
        }
64
65
        return V.h('tr', [
66
          V.h('td', icons),
67
          V.h('td', nodeLink(n.node)),
68
          V.h('td', n.node.clients),
69
          V.h('td', { style: { color: linkScale((n.link.source_tq + n.link.target_tq) / 2) } }, helper.showTq(n.link.source_tq) + ' - ' + helper.showTq(n.link.target_tq)),
70
          V.h('td', helper.showDistance(n.link))
71
        ]);
72
      }
73
74
      var self = this;
75
      var header = document.createElement('h2');
76
      var table = document.createElement('table');
77
      var images = document.createElement('div');
78
      var neighbours = document.createElement('h3');
79
      var headings = [{
80
        name: ''
81
      }, {
82
        name: 'node.nodes',
83
        sort: function (a, b) {
84
          return a.node.hostname.localeCompare(b.node.hostname);
85
        },
86
        reverse: false
87
      }, {
88
        name: 'node.clients',
89
        class: 'ion-people',
90
        sort: function (a, b) {
91
          return a.node.clients - b.node.clients;
92
        },
93
        reverse: true
94
      }, {
95
        name: 'node.tq',
96
        class: 'ion-connection-bars',
97
        sort: function (a, b) {
98
          return a.link.source_tq - b.link.source_tq;
99
        },
100
        reverse: true
101
      }, {
102
        name: 'node.distance',
103
        class: 'ion-arrow-resize',
104
        sort: function (a, b) {
105
          return (a.link.distance === undefined ? -1 : a.link.distance) -
106
            (b.link.distance === undefined ? -1 : b.link.distance);
107
        },
108
        reverse: true
109
      }];
110
      var tableNeighbour = new SortTable(headings, 1, renderNeighbourRow);
111
112
      el.appendChild(header);
113
      el.appendChild(table);
114
      el.appendChild(neighbours);
115
      el.appendChild(tableNeighbour.el);
116
      el.appendChild(images);
117
118
      self.render = function render() {
119
        V.patch(header, V.h('h2', d.hostname));
120
121
        var children = [];
122
123
        config.nodeAttr.forEach(function (row) {
124
          var field = d[row.value];
125
          if (nodef['show' + row.value] !== undefined) {
126
            field = nodef['show' + row.value](d);
127
          }
128
129
          if (field) {
130
            if (typeof field !== 'object') {
131
              field = V.h('td', field);
132
            }
133
134
            children.push(V.h('tr', [
135
              V.h('th', _.t(row.name)),
136
              field
137
            ]));
138
          }
139
        });
140
141
142
        children.push(V.h('tr', [
143
          V.h('th', _.t('node.gateway')),
144
          showGateway(d)
145
        ]));
146
147
        var elNew = V.h('table', children);
148
        table = V.patch(table, elNew);
149
        table.elm.classList.add('attributes');
150
151
        V.patch(neighbours, V.h('h3', _.t('node.link', d.neighbours.length) + ' (' + d.neighbours.length + ')'));
152
        if (d.neighbours.length > 0) {
153
          tableNeighbour.setData(d.neighbours);
154
          tableNeighbour.el.elm.classList.add('node-links');
155
        }
156
157
        if (config.nodeInfos) {
158
          var img = [];
159
          config.nodeInfos.forEach(function (nodeInfo) {
160
            img.push(V.h('h4', nodeInfo.name));
161
            img.push(showStatImg(nodeInfo, d));
162
          });
163
          images = V.patch(images, V.h('div', img));
164
        }
165
      };
166
167
      self.setData = function setData(data) {
168
        if (data.nodeDict[d.node_id]) {
169
          d = data.nodeDict[d.node_id];
170
        }
171
        self.render();
172
      };
173
      return self;
174
    };
175
  });
176